package com.NxIndustries.Sapphire;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;

import com.htc.configuration.HtcWrapConfiguration;
import com.htc.widget.ActionBarContainer;
import com.htc.widget.ActionBarExt;
import com.htc.widget.ActionBarText;
import com.htc.widget.SlidingMenu;

/**
 * Wallpaper picker for the Home application. User can choose from
 * a gallery of stock photos.
 */
@SuppressWarnings("deprecation")
public class Wallpapers extends Activity implements
        AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener,
        OnClickListener {
    
    private static final String LOG_TAG = "Sapphire Wallpapers";

    private static final Integer[] THUMB_IDS = {
            R.drawable.s_one_small,
            R.drawable.s_two_small,
            R.drawable.s_three_small,
            R.drawable.s_four_small,
            R.drawable.s_five_small,
            R.drawable.s_six_small,
    };

    private static final Integer[] IMAGE_IDS = {
    	R.drawable.s_one,
        R.drawable.s_two,
        R.drawable.s_three,
        R.drawable.s_four,
        R.drawable.s_five,
        R.drawable.s_six,
    };

    private Gallery mGallery;
    private boolean mIsWallpaperSet;
    
    private ActionBarExt actionBarExt=null;
   	private ActionBarText actionBarText=null;    
   	private ActionBarContainer actionBarContainer=null;
    
    public static int getHtcThemeID(Context context, int i) {
   		return HtcWrapConfiguration.getHtcThemeId(context, i);
       }
       private void SetupActionBar() {
           actionBarExt=new ActionBarExt(this,getActionBar());
           actionBarContainer=actionBarExt.getCustomContainer();
           actionBarText=new ActionBarText(this);    		        
   	    actionBarText.setPrimaryText("Sapphire Wallpaper Chooser");   
   	    actionBarContainer.addCenterView(actionBarText);
        }
        
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        Toast.makeText(getApplicationContext(),
        "Swipe Right, to enable Menu",
        Toast.LENGTH_SHORT).show();
        
        Context context = this;
		setTheme(getHtcThemeID(context, 0));
        
        setContentView(R.layout.wallpaper_chooser);
        SetupActionBar();

        mGallery = (Gallery) findViewById(R.id.gallery);
        mGallery.setAdapter(new ImageAdapter(this));
        mGallery.setOnItemSelectedListener(this);
        mGallery.setOnItemClickListener(this);
        mGallery.setCallbackDuringFling(false);
        
        SlidingMenu mSlidingMenu = new SlidingMenu(this);
        mSlidingMenu.setMode(SlidingMenu.LEFT);
        mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width);
		mSlidingMenu.setBehindWidth(700);
		mSlidingMenu.setFadeDegree(0.35F);
		mSlidingMenu.setFadeEnabled(true);
		mSlidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        mSlidingMenu.setMenu(R.layout.wallpaper_menu);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        mIsWallpaperSet = false;
    }

    public void onItemSelected(AdapterView parent, View v, int position, long id) {
        getWindow().setBackgroundDrawableResource(IMAGE_IDS[position]);
    }
    
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        selectWallpaper(position);
    }

    /*
     * When using touch if you tap an image it triggers both the onItemClick and
     * the onTouchEvent causing the wallpaper to be set twice. Synchronize this
     * method and ensure we only set the wallpaper once.
     */
    private void selectWallpaper(int position) {
        if (mIsWallpaperSet) {
            return;
        }
        mIsWallpaperSet = true;
        try {
            InputStream stream = getResources().openRawResource(IMAGE_IDS[position]);
            setWallpaper(stream);
            setResult(RESULT_OK);
            finish();
        } catch (IOException e) {
            Log.e(LOG_TAG, "Failed to set wallpaper " + e);
        }
    }

    public void onNothingSelected(AdapterView parent) {
    }

    public class ImageAdapter extends BaseAdapter {

        private Context mContext;
        
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return THUMB_IDS.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            i.setImageResource(THUMB_IDS[position]);
            i.setAdjustViewBounds(true);
            i.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            i.setBackgroundResource(R.drawable.grid);
            return i;
        }
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.wallpaper_menu, menu);
      return super.onCreateOptionsMenu(menu);
    } 
    
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.finish:
		Wallpapers.this.finish();
	}
	return false;
    }
	@Override
	public void onClick(View v) {
		selectWallpaper(mGallery.getSelectedItemPosition());
		
	}
}

    